//
//  ZGLPlayHotButtonVC.m
//  Faker
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLPlayHotButtonVC.h"
#import "ZGLHTTPTOOL.h"
#import <UIImageView+AFNetworking.h>
#import <SDWebImage/UIButton+WebCache.h>
#import "ZGLPlayHomeSecondTVC.h"
#import "ZGLPlayDetailModel.h"
#import "ZGLPlayDetailWebVC.h"
#import "ZGLPlayLocationVC.h"
#import "ZGLmyVC.h"


#pragma mark 1 - 声明属性 签协议
@interface ZGLPlayHotButtonVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
/* 左边bar属性 */
@property (nonatomic, strong) UIBarButtonItem *barButtonItem;
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
@end

@implementation ZGLPlayHotButtonVC

/* 视图将要出现时 改变导航栏颜色*/
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tableView.backgroundColor = backColor;
    self.view.backgroundColor = backColor;
    [self.tableView reloadData];
   
    self.navigationController.navigationBar.barTintColor = naviColor;
    
}


/* 内存管理 */
-(void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}
#pragma mark 2 - 主程序入口
- (void)viewDidLoad {
    [super viewDidLoad];
    /* 创建creatTavleView */
    [self creatTableView];
    /* 创建导航栏 */
   [self designNavigationBar];
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 60 * ScaleX, 40 * ScaleX)];
    self.activityIndicatorView.center = self.view.center;
    [self.view addSubview:self.activityIndicatorView];
    self.activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [self.activityIndicatorView startAnimating];
    // Do any additional setup after loading the view.
}
#pragma mark 3 - 属性set方法重写
-(void)setModelArr:(NSMutableArray *)modelArr {
    if (_modelArr != modelArr) {
        _modelArr = modelArr;
    }
    [self.activityIndicatorView stopAnimating];
    [self.tableView reloadData];
}
-(void)setCityName:(NSString *)cityName {
    if (_cityName != cityName) {
        _cityName = cityName;
    }
    /* 赋值给bar 作为标题 */
    self.barButtonItem.title = cityName;
    
}
#pragma mark 4 - tableView部分
/* 创建部分 */
-(void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, Height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[ZGLPlayHomeSecondTVC class] forCellReuseIdentifier:@"ZGLPlayHotButtonVC_Cell"];
    
}
/* 行数 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArr.count;
}
/* 分区数 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/* cell赋值部分 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZGLPlayDetailModel *model = [self.modelArr objectAtIndex:indexPath.row];
    ZGLPlayHomeSecondTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLPlayHotButtonVC_Cell"];
    cell.titleLabel.text = model.title;
    cell.contentLabel.text = model.s_title;
    [cell.mainImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", model.thumbnail_medias[0][@"url"]]] placeholderImage:[UIImage imageNamed:@"place1.jpg"]];
    return cell;
    
   }
/* 行高 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 250 * ScaleX;
   }
/* Header高度设置 */
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
    }
#pragma mark 5 - 设计NavigationBar
-(void)designNavigationBar {
    /* 左边标题设置 */
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] init];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem.title = self.itemName;
    /* 右边两个item设计 */
    //定位
    self.barButtonItem = [[UIBarButtonItem alloc] initWithTitle:self.cityName style:UIBarButtonItemStyleDone target:self action:@selector(location)];
    self.barButtonItem.tintColor = [UIColor whiteColor];
    //个人中心
    UIBarButtonItem *personBar = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"lianxiren"]  style:UIBarButtonItemStyleDone target:self action:@selector(personBar)];
    personBar.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItems = @[personBar, self.barButtonItem];
}
#pragma mark 4 ** 右边两个bar点击事件
-(void)personBar {
    ZGLmyVC *myVC = [[ZGLmyVC alloc] init];
    [self.navigationController pushViewController:myVC animated:YES];
}
-(void)location {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 5 - cell点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZGLPlayDetailModel *model = [self.modelArr objectAtIndexedSubscript:indexPath.row];
    ZGLPlayDetailWebVC *webVC = [[ZGLPlayDetailWebVC alloc] init];
    webVC.webStr = model.weekend[@"content_url"];
    [self.navigationController pushViewController:webVC animated:YES];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
